package nc.tb.form;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import nc.tb.cube.cell.InnerCell;
import nc.tb.pub.util.SortTool;



public class CellSetCheckSum implements java.io.Serializable,Cloneable{
	private static final long serialVersionUID = 13366288289L;
	public String[] planPKs = null;
	public String[] formulaCellPS  = null;
	public HashMap hmCellPosInfo = null;
	public CellSetCheckSum(){
		
	}
	public CellSetCheckSum(ArrayList cells,String strLocalPlanPK){
		if(cells == null || cells.isEmpty())
			return;
		HashMap hmTemp = new HashMap();
		ArrayList alFormulaCells = new ArrayList();
		ArrayList alPlanPKs = new ArrayList(); 
		for (int i = 0; i < cells.size(); i++) {
			InnerCell ic = (InnerCell)cells.get(i);
			String strPlanPK = ic.getParentSheetObject().getPrimaryKey();
			if(strLocalPlanPK.equals(strPlanPK)){
				if(ic.getCellFormula() != null && ic.getCellFormula().isHasCaculateFormula()){
					alFormulaCells.add(ic.getAreaPosition().getStart().toString());
				}
			}else{
				if(!alPlanPKs.contains(strPlanPK)){
					alPlanPKs.add(strPlanPK);
				}
				ArrayList alCells = (ArrayList)hmTemp.get(strPlanPK);
				if(alCells == null){
					alCells = new ArrayList();
					hmTemp.put(strPlanPK,alCells);
				}
				alCells.add(ic);
			}
		}
		formulaCellPS = (String[]) alFormulaCells.toArray(new String[0]);
		if(alPlanPKs.isEmpty() || hmTemp.isEmpty())
			return ;
		planPKs = (String[]) alPlanPKs.toArray(new String[0]);		
		Arrays.sort(planPKs);
		for(int i = 0; i < planPKs.length; i ++){
			ArrayList alCells = (ArrayList)hmTemp.remove(planPKs[i]);
			if(alCells == null || alCells.isEmpty())
				continue ;
			SortTool.sortInnerCellByPosition(alCells,true,false);
			String[] cellPos = new String[alCells.size()];
			for(int j = 0; j < cellPos.length; j ++){
				InnerCell ic = (InnerCell)alCells.get(j);
				cellPos[j] = ic.getAreaPosition().getEnd().toString();
			}
			hmTemp.put(planPKs[i],cellPos);
		}
		hmCellPosInfo = hmTemp;
	}
	public boolean isEmpty(){
		return (planPKs == null|| planPKs.length == 0 ) && (hmCellPosInfo == null || hmCellPosInfo.isEmpty())
			&& (formulaCellPS == null || formulaCellPS.length == 0);
	}
	public boolean equals(CellSetCheckSum ccs){
		if(ccs == null)
			return false;
		if(isEmpty() && ccs.isEmpty())
			return true;
		String[] PKs = ccs.planPKs;
		HashMap cellPosInfo = ccs.hmCellPosInfo;
		if(!ArrayEquals(planPKs,PKs))
			return false;
		if(!ArrayEquals(formulaCellPS,ccs.formulaCellPS))
			return false;
		for(int i = 0; i < planPKs.length; i ++){
			String strPlanPK = planPKs[i];
			String[] pos1 = (String[])hmCellPosInfo.get(strPlanPK);
			String[] pos2 = (String[])cellPosInfo.get(strPlanPK);
			if(!ArrayEquals(pos1,pos2))
				return false;
		}
		return true;
	}
	private static boolean ArrayEquals(String[] a1,String[] a2){
		if(a1 == null && a2 == null)
			return true;
		if((a1 == null && a2 != null)
				||(a1 != null && a2 == null))
			return false;
		if(a1.length != a2.length)
			return false;
		for(int i  = 0; i < a1.length; i ++){
			if(!a1[i].equals(a2[i]))
				return false;
		}
		return true;
	}
	public String[] getFormulaCellPS() {
		return formulaCellPS;
	}
}